package com.ms3ds.mineInfo.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ms3ds.common.core.utils.DateUtils;
import com.ms3ds.common.security.utils.SecurityUtils;
import com.ms3ds.mineInfo.api.domain.Team;
import com.ms3ds.mineInfo.mapper.TeamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ms3ds.mineInfo.mapper.WorkerMapper;
import com.ms3ds.mineInfo.api.domain.Worker;
import com.ms3ds.mineInfo.service.IWorkerService;

/**
 * 班组Service业务层处理
 * 
 * @author zlw
 * @date 2025-09-18
 */
@Service
public class WorkerServiceImpl implements IWorkerService 
{
    @Autowired
    private WorkerMapper workerMapper;

    @Autowired
    private TeamMapper teamMapper;

    /**
     * 查询班组
     * 
     * @param id 班组主键
     * @return 班组
     */
    @Override
    public Worker selectWorkerById(Long id)
    {
        return workerMapper.selectWorkerById(id);
    }

    /**
     * 查询班组列表
     * 
     * @param worker 班组
     * @return 班组
     */
    @Override
    public List<Worker> selectWorkerList(Worker worker)
    {
        return workerMapper.selectWorkerList(worker);
    }

    /**
     * 新增班组
     * 
     * @param worker 班组
     * @return 结果
     */
    @Override
    public int insertWorker(Worker worker)
    {
        Long userId = SecurityUtils.getUserId();
        worker.setCreateBy(String.valueOf(userId));
        worker.setCreateTime(DateUtils.getNowDate());
        return workerMapper.insertWorker(worker);
    }

    /**
     * 修改班组
     * 
     * @param worker 班组
     * @return 结果
     */
    @Override
    public int updateWorker(Worker worker)
    {
        Long userId = SecurityUtils.getUserId();
        worker.setUpdateBy(String.valueOf(userId));
        worker.setUpdateTime(DateUtils.getNowDate());
        return workerMapper.updateWorker(worker);
    }

    /**
     * 批量删除班组
     * 
     * @param ids 需要删除的班组主键
     * @return 结果
     */
    @Override
    public int deleteWorkerByIds(Long[] ids)
    {
        return workerMapper.deleteWorkerByIds(ids);
    }

    /**
     * 删除班组信息
     * 
     * @param id 班组主键
     * @return 结果
     */
    @Override
    public int deleteWorkerById(Long id)
    {
        return workerMapper.deleteWorkerById(id);
    }

    @Override
    public List<Map<String,Object>> getSystemPreviewWorker(Long mineId) {
        Team searchTeam = new Team();
        searchTeam.setMineId(mineId);
        List<Team> teams = teamMapper.selectTeamList(searchTeam);
        List<Map<String,Object>> list = new ArrayList<>();
        for(Team team : teams){
            Map<String,Object> map = new HashMap<>();
            //统计生产队下有多少班组
            int count = workerMapper.countWorkersByTeamId(team.getId());
            map.put("value", count);
            map.put("name",team.getTeamName());
            list.add(map);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getDecisionSupportWorker(Long mineId) {
        List<Worker> workers = workerMapper.selectWorkerByMineId(mineId);
        List<Map<String, Object>> list = new ArrayList<>();
        for(Worker worker : workers){
            Map<String,Object> map = new HashMap<>();
            map.put("id", worker.getId());
            map.put("name", worker.getWorkerName());
            map.put("teamName", worker.getTeamName());
            map.put("status", worker.getStatus()==1?"on":"off");
            list.add(map);
        }
        return list;
    }
}
